package com.fxu.framework.biz.util;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.fxu.framework.biz.rest.impl.ApiProxyRestImpl;
import com.fxu.framework.core.base.Constant;
import com.fxu.framework.core.holder.RequestHolder;
import org.apache.tomcat.util.http.MimeHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;

/**
 * Token的操作基础类
 * @author fangxu
 * @version 1.0.0
 */
//@Component
public final class TokenUtil {
	/**
	 * 获得Token，有可能返回null，兼容Token和Authorization头
	 * @param request 请求对象
	 * @return 返回Token
	 */
	public static String getToken(HttpServletRequest request) {
		if (null == request) return null;
		String token = request.getHeader(Constant.HEADER_TOKEN);
		return null != token ? token : request.getHeader(Constant.HEADER_AUTH);
	}

	/**
	 * 获取操作者
	 * @return 操作者名
	 */
	public static JWT getJwt(String token) {
		if (token == null) return null;
		JWT jwt = null;
		try {
			jwt = JWTUtil.parseToken(token);
		} catch (Exception e) {
			//log
		}
		return jwt;
	}

	/**
	 * 获取Jwt中的值
	 * @return Jwt中的值
	 */
	public static String getJwtVal(String token, String key) {
		JWT jwt = getJwt(token);
		if (jwt == null) return "";
		Object value = jwt.getPayload(key);
		return value == null ? "" : String.valueOf(value);
	}

	/**
	 * 获取操作者
	 * @return 操作者名
	 */
	public static String getOperator() {
		HttpServletRequest request = RequestHolder.getRequestMayNull();
		if (request == null) return "";
		JWT jwt = getJwt(getToken(request));
		if (jwt == null) return "";
		Object name = jwt.getPayload(LoginUtil.TOKEN_KEY_NAME);
		if (name == null) {
			// 兼容jeecg
			name = jwt.getPayload("username");
		}
		return name == null ? "" : String.valueOf(name);
	}

	/**
	 * 修改header信息，key-value键值对儿加入到header中
	 * @param request 请求对象
	 * @param key 设置header的key
	 * @param value 设置header的value
	 */
	public static void reflectSetHeader(HttpServletRequest request, String key, String value) {
		Class<? extends HttpServletRequest> requestClass = request.getClass();
		try {
			Field request1 = requestClass.getDeclaredField("request");
			request1.setAccessible(true);
			Object o = request1.get(request);
			Field coyoteRequest = o.getClass().getDeclaredField("coyoteRequest");
			coyoteRequest.setAccessible(true);
			Object o1 = coyoteRequest.get(o);
			Field headers = o1.getClass().getDeclaredField("headers");
			headers.setAccessible(true);
			MimeHeaders o2 = (MimeHeaders) headers.get(o1);
			int index = o2.findHeader(key, 0);
			if (index > -1) {
				o2.getValue(index).setString(value);
			} else {
				o2.addValue(key).setString(value);
			}
		} catch (Exception e) {
			throw new RuntimeException("重写报头失败!e=" + e.getMessage());
		}
	}
	
	/**
	 * 修改header信息，key-value键值对儿加入到header中
	 * @param request 请求对象
	 * @param value 设置Toke的value
	 */
	public static void setHeaderToken(HttpServletRequest request, String value) {
		reflectSetHeader(request, Constant.HEADER_TOKEN, value);
	}
}
